import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:schoolassets/%E7%9B%98%E7%82%B9/%E7%9B%98%E7%82%B9%E8%AE%A1%E5%88%92%E8%AF%A6%E6%83%85/Inventory.dart';
import 'package:schoolassets/%E9%A2%86%E7%94%A8/%E7%89%A9%E8%B5%84%E9%A2%86%E7%94%A8%E8%AF%A6%E6%83%85/LingYong.dart';
import 'package:schoolassets/Tools%E5%B7%A5%E5%85%B7/ShowToast.dart';
import 'package:schoolassets/Tools%E5%B7%A5%E5%85%B7/requestUrl.dart';

/**
 * Creat For 11643
 * CreatTime 2022/12/28
 * Description:
 */
class LingYongList extends StatefulWidget {
  const LingYongList({Key? key}) : super(key: key);

  @override
  _LingYongListState createState() => _LingYongListState();
}

class _LingYongListState extends State<LingYongList> {
  var files = [];
  ///分组入库信息列表
  List<DataRow> KuMessage = [];
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      // 获取盘点计划
      GetRfidPlan();
    });
    // 加载本地文件
    // LoadFile();
  }

  ///页面卸载
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  ///当 State 被暂时从视图树中移除时，会调用这个方法，同时页面切换时，也会调用。
  @override
  void deactivate() {
    // TODO: implement dispose
    super.deactivate();
  }

  ///在 initState() 之后调用，当 State 对象的依赖关系发生变化时，该方法被调用，初始化时也会调用。
  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text("物资领用单", style: TextStyle(fontSize: 15)),
            elevation: 0,
          actions: [
            IconButton(onPressed: (){
              // 获取盘点计划
              GetRfidPlan();
            }, icon: Icon(Icons.refresh))
          ],
        ),
        body: Container(
          color: Colors.white,
          child: Column(
            children: [
              ///中间文字
              Container(
                padding: EdgeInsets.only(left: 15,right: 15,top: 15),
                alignment: Alignment.topLeft,
                child: Text("盘点计划单列表:",style: TextStyle(fontSize: 17,fontWeight: FontWeight.w600),),
              ),
              Expanded(
                  child: ListView.builder(
                      itemCount: files.length,
                      itemBuilder: (context, index) {
                        return buildListViewItem(index);
                      }))
            ],
          ),
        ),// This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  // 文件列表样式
  Widget buildListViewItem(index) {
    return Column(
      children: <Widget>[
        Container(
          padding: EdgeInsets.all(10),
          decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(width: 1, color: Color(0xfff2f2f2)))),
          child: ListTile(
            leading: Image.asset("Image/wenjian.png",width: 40,height: 40,),
            title: Container(
              alignment: Alignment.topLeft,
              child: Text(
                "${files[index]['ID']}",
                style: TextStyle(fontSize: 15),
              ),
            ),
            subtitle: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "领用进度：${files[index]['UseProgress']}/${files[index]['UseCount']}",
                  style: TextStyle(
                      color: files[index]['UseProgress'] ==
                          files[index]['UseCount']
                          ? Colors.green
                          : Colors.red),
                ),
                Text("申请日期：${files[index]['CREATED_TIME']}")
              ],),
            contentPadding: EdgeInsets.all(5),
            onTap: () {
              print("盘点单号:${files[index]['ID']}");
              // 前往盘点页面
              Navigator.of(context).push(new MaterialPageRoute(builder: (_)=>LingYong(TypeList:json.encode(files[index]['exAssetUseTypes']) ,AssetUseID:files[index]['ID'])),).then((val){
                if(val=="reflash"){
                  GetRfidPlan();
                }

              });
            },
          ),
        )
      ],
    );
  }

// 获取领用计划
  Future<void> GetRfidPlan() async {
    var ResponseData;
    files.clear();
    ResponseData = await new requestUrl().CommenRequest(
        '/api/AppAssetUse/Get', 'post', {}, "拉取数据中...", context, true,"");
    if (ResponseData['code'] == 0) {
      files = ResponseData['data'];
    } else {
      ShowToast().MyToast(ResponseData['msg']);
    }
    setState(() {});
  }

}